import { Injectable } from '@nestjs/common'
import { CreateAccountDto } from './dto/create-account.dto'
import { UpdateAccountDto } from './dto/update-account.dto'
import { Equal, Repository, Not, IsNull } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { Account } from './entities/account.entity'
import { formatOptionData } from 'src/utils'
@Injectable()
export class AccountService {
  constructor(
    @InjectRepository(Account) private readonly account: Repository<Account>
  ) {}
  create(createAccountDto: CreateAccountDto) {
    const account = new Account()
    account.lunchSalary = createAccountDto.lunchSalary
    account.trafficSalary = createAccountDto.trafficSalary
    account.basisSalary = createAccountDto.basisSalary
    account.allSalary =
      createAccountDto.lunchSalary +
      createAccountDto.trafficSalary +
      createAccountDto.basisSalary
    account.name = createAccountDto.name
    return this.account.save(account)
  }

  async findAll(params: {
    page: number
    pageSize: number
    type?: string
    isDelete?: boolean
  }) {
    const where = {}
    params.isDelete && (where['deleteDate'] = Not(IsNull()))
    const data = await this.account.find({
      where: { ...where },
      withDeleted: params.isDelete,
      skip: (params.page - 1) * params.pageSize,
      take: params.pageSize
    })
    const total = await this.account.count({
      where: { ...where },
      withDeleted: params.isDelete
    })
    return { data, total }
  }

  async findAllOption() {
    const data = await this.account.find()
    return formatOptionData(data)
  }

  update(id: number, updateAccountDto: UpdateAccountDto) {
    return this.account.update(id, updateAccountDto)
  }

  remove(ids: number[]) {
    return this.account.softDelete(ids)
  }

  thoroughRemove(ids: number[]) {
    return this.account.delete(ids)
  }

  restore(ids: number[]) {
    return this.account.restore(ids)
  }
}
